<template>
  <div>
    <div>
      <el-input
        placeholder="请输入"
        style="width: 300px; float: right"
        v-model="pagination.search"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getList"
        ></el-button>
      </el-input>
      <!-- <el-button type="danger" @click="deleteRow">测试删除所有记录</el-button>
      <el-button type="danger" @click="addAPPVisible = true">添加APP</el-button> -->
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column :prop="item.key" :label="item.label" v-for="item in headers" :key="item.key"></el-table-column>
      <!-- <el-table-column prop="APPID" label="APPID"></el-table-column> -->
      <!-- <el-table-column label="当前版本">
        <template slot-scope="scope">
          {{ scope.row.edition[scope.row.edition.length - 1].value }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="onCheck(scope.row)">查看</el-button>
          <el-button type="info" plain size="mini" @click="onBook(scope.row)"
            >更新版本</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <div>
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="pagination.total"
        :page-size="pagination.size"
        :current-page="pagination.current"
        @current-change="currentChange"
      ></el-pagination>
    </div>
    <!-- <div>
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="pagination.total"
        :page-size="pagination.size"
        :current-page="pagination.current"
        @current-change="currentChange"
      ></el-pagination>
    </div>
    <el-dialog title="查看" :visible.sync="seeAPPVisible">
      <div slot="footer" class="dialog-footer">
        <el-button @click="addAPPVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAPP()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加APP" :visible.sync="addAPPVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="APP名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="APPID" prop="APPID">
          <el-input v-model="form.APPID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上传文件">
          <uploader
            :options="options"
            class="uploader-example"
            @file-success="onFileSuccess"
          >
            <uploader-unsupport></uploader-unsupport>
            <uploader-drop>
              <p>选择最新的apk上传</p>
              <uploader-btn :attrs="attrs">选择apk文件</uploader-btn
              >//文件夹上传
            </uploader-drop>
            <uploader-list></uploader-list>
          </uploader>
        </el-form-item>
        <div v-for="(item, i) in form.edition" :key="i">
          <el-form-item label="版本号" prop="edition.value">
            <el-input v-model="item.value" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="下载链接" prop="edition.url">
            <el-input v-model="item.url" autocomplete="off" disabled></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="edition.content">
            <el-input
              v-model="item.content[index]"
              autocomplete="off"
              v-for="(content, index) in item.content"
              :key="index"
              @input="onInput(item.content, index)"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addAPPVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAPP()">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

  <script>
export default {
  components: {},
  data() {
    return {
      addAPPVisible: false,
      seeAPPVisible: false,
      infoData: {},
      tableData: [],
      headers: [],
      check: false,
      pagination: {
        total: 10,
        current: 1,
        size: 10,
        search: "",
      },
      form: {
        name: "",
        APPID: "",
        edition: [
          {
            value: "",
            content: [""],
            url: "",
          },
        ],
      },
      options: {
        target: this.$URL + "uploadapp",
        singleFile: true, //是否单文件上传
        testChunks: false,
        chunkSize: 1024 * 1024 * 30, //1MB
        simultaneousUploads: 3, //并发上传数
        extension: "apk",
        headers: {
          Authorization: `Bearer ${sessionStorage.getItem("token")}`,
        },
      },
      attrs: {
        accept: ".apk", //选择文件的格式
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onInput(item, index) {
      if (item.length - 1 == index) {
        item.push("");
      }
    },
    onFileSuccess(rootFile, file, response, chunk) {
      console.log(chunk.processedState.res);
      this.form.edition[this.form.edition.length - 1].url =
        chunk.processedState.res;
    },
    async addAPP() {
      console.log(this.$refs.form);
      var res = await this.$http.post(`/APP`, this.form);
      if (res.status) {
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.addAPPVisible = false;
        this.form = {
          name: "",
          APPID: "",
          edition: [
            {
              value: "",
              content: [""],
              url: "",
            },
          ],
        };
        this.getList();
      } else {
        this.$message({
          message: "上传失败",
          type: "success",
        });
      }

      // console.log(res);
    },
    onBook(e) {
      this.infoData = e;
      this.Confirmation = true;
    },
    onCheck(e) {
      console.log("e", e);
      this.infoData = e;
      this.check = true;
    },
    onSearch(e) {
      this.getList({ name: e });
    },
    currentChange(e) {
      this.pagination.current = e;
      this.getList();
    },
    async getList() {
      let data = JSON.stringify(this.pagination);
      console.log(data);
      //   if (pages) {
      //     data = pages;
      //   }
      //   const list = await this.$http.get(`/cat`, data);
      const list = await this.$http.get(`/logData?search=${data}`);
      console.log(list);
      if (list.status == 200) {
						this.tableData = list.data.list.map((e) => {
							return e.log
						})
						this.headers = Object.keys(this.tableData[0]).map(e => {
							return {
								label: e,
								key: e
							}
						})
					}
      this.pagination = {
        total: list.data.total,
        current: list.data.current,
        size: list.data.size,
      };
      // this.tableData = list.data.list;
    },
    deleteRow() {
      this.$confirm(`此操作将永久删除所有记录, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await this.$http.delete(`/APP`);
        this.getList();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
  },
};
</script>

<style>
.el-table .cell {
  text-align: center !important;
}
</style>